/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.trandio.manager;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.trandio.dao.ActividadDAO;
import org.trandio.dao.ActividadInterface;
import org.trandio.dao.MedicoDAO;
import org.trandio.dao.MedicoInterface;
import org.trandio.hb.TbActividad;
import org.trandio.model.ActividadVO;
import org.trandio.model.MedicoVO;

/**
 *
 * @author root
 */
public class MedicoServlet extends HttpServlet {

    MedicoVO med = new MedicoVO();
    List turnos;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            MedicoInterface iface = new MedicoDAO();
            ActividadInterface ifaceAct = new ActividadDAO();

            //BUSCAR EL TURNO A FACTURAR PARA LA FECHA DE LA CITA
            /*
             * String fec = request.getParameter("fechaCita");
             *
             * DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date fecha =
             * null; try { fecha = df.parse(request.getParameter("fechaCita"));
             * } catch (ParseException ex) {
             * Logger.getLogger(MedicoServlet.class.getName()).log(Level.SEVERE,
             * null, ex);
            }
             */

            turnos = iface.traerTurnos(Integer.parseInt(request.getParameter("paciente")));


            if (turnos.size() > 0) {

                Iterator it = turnos.iterator();
                out.println("<table id='tbl_turnos'>");
                out.println("<tr>");
                out.println("<td>&nbsp;</td><td>Turno</td><td>Especialidad</td><td>Médico</td><td>Actividad</td><td style='display:none'>&nbsp;</td><td style='display:none'>&nbsp;</td><td style='display:none'>&nbsp;</td>");
                out.println("</tr>");
                while (it.hasNext()) {
                    Object[] object = (Object[]) it.next();
                    try {
                        med = iface.getMedico(Integer.parseInt(object[0].toString()));
                    } catch (SQLException ex) {
                        Logger.getLogger(MedicoServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(MedicoServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    
                    out.println("<tr>");
                    out.println("<td>");
                    out.println("<input type='checkbox' id='" + object[6] + "' />");
                    out.println("</td>");
                    
                    out.println("<td>");
                    out.println(object[1] + " " + object[4] + "-" + object[5]);
                    out.println("</td>");
                    
                    out.println("<td>");
                    out.println(object[2]);
                    out.println("</td>");
                    
                    out.println("<td>");
                    out.println(med.getNombre());
                    out.println("</td>");
                    
                    out.println("<td>");

                    List<ActividadVO> listAct = new ArrayList<ActividadVO>();

                    try {
                        listAct = ifaceAct.traerConsultas(Integer.parseInt(request.getParameter("idcps")));
                    } catch (SQLException ex) {
                        Logger.getLogger(MedicoServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(MedicoServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    Iterator act = listAct.iterator();
                    out.println("<select style= 'width: 200px' id='act' onchange='$(\"#acttmp_" + object[6] + "\").text(this.value)'>");

                    out.println("<option value=''>:: Seleccione ::</option>");
                    float tarifaAct = 0;
                    while (act.hasNext()) {
                        ActividadVO ac = (ActividadVO) act.next();

                        try {
                            tarifaAct = ifaceAct.consultarTarifa(request.getParameter("manual"), ac.getId() + "",0);
                        } catch (SQLException ex) {
                            Logger.getLogger(MedicoServlet.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (NamingException ex) {
                            Logger.getLogger(MedicoServlet.class.getName()).log(Level.SEVERE, null, ex);
                        }

                        out.println("<option value='" + ac.getId() + "|" + ac.getCodigo() + "|" + ac.getDescripcion() + "|1|" + tarifaAct + "|" + med.getId() + "|" + ac.getGrupo_funcional() + "'>" + ac.getCodigo() + " " + ac.getDescripcion() + "</option>");
                    }
                    out.println("</select>");
                    out.println("</td>");
                    out.println("<td style='display:none' id='acttmp_" + object[6] + "' >");
                    out.println("</td>");
                    out.println("<td style='display:none'>" + object[6] + "</td>");
                    out.println("</td>");
                    out.println("<td style='display:none'>" + object[7] + "</td>");
                    out.println("</tr>");
                }
                out.println("</table>");
            } else {
                //out.println("carlos");
            }



        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
